package com.rentalcar.rental_car.controller;

import com.rentalcar.rental_car.entity.Orders;
import com.rentalcar.rental_car.entity.Result;
import com.rentalcar.rental_car.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @PostMapping("/orderList")
    public Result orderList(){
        try {
            log.info("查询所有订单");
            return Result.success(ordersService.getOrderAll());
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败");
        }
    }
    @PostMapping("/selectCustKey")
    public Result selectCustKey(@RequestBody Orders orders){
        try {
            log.info("查询"+orders.getCustKey()+"的所有订单");
            return Result.success(ordersService.selectByCustKey(orders.getCustKey()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败");
        }
    }
    @PostMapping("/selectStatus")
    public Result selectStatus(@RequestBody Orders orders){
        try {
            log.info("查找所有"+orders.getStatus()+"状态的订单");
            return Result.success(ordersService.selectByStatus(orders.getStatus()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败");
        }
    }
    @PostMapping("/addOrders")
    public Result addOrders(@RequestBody Orders orders){
        try {
            ordersService.add(orders);
            log.info("添加一个订单");
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加订单失败");
        }
    }
    @PostMapping("/updateOrders")
    public Result updateOrders(@RequestBody Orders orders){
        try {
            ordersService.update(orders);
            log.info("更新"+orders.getOrderKey()+"订单");
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }
    @PostMapping("/deleteOrders")
    public Result deleteOrders(@RequestBody Orders orders){
        try {
            ordersService.delete(orders);
            log.info("删除"+orders.getOrderKey()+"订单");
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/getMount")
    public Result getMount(){
        try {
            log.info("查询各月数据");
            return Result.success(ordersService.getMountByMonth());
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail();
    }
}
